/**
 * Created by Max-Goday on 15/7/23.
 */

function noty(type, title, msg) {
	new PNotify({
		title: title,
		text: msg,
		type: type,
		animation: 'show',
		delay: 2000,
		icon: false
	});
}

window.alert = function(message) {
	noty('Info', 'Info', message);
};

function noty_process() {
	var cur_value = 1,
		progress;

	// Make a loader.
	var loader = new PNotify({
		//title: "Lowering the Moon",
		title: "Creating Series of Tubes",
		text: '<div class="progress progress-striped active" style="margin:0">\
					<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0">\
						<span class="sr-only">0%</span>\
					</div>\
				</div>',
		//icon: 'fa fa-moon-o fa-spin',
		icon: 'fa fa-cog fa-spin',
		hide: false,
		buttons: {
			closer: false,
			sticker: false
		},
		history: {
			history: false
		},
		before_open: function(PNotify) {
			progress = PNotify.get().find("div.progress-bar");
			progress.width(cur_value + "%").attr("aria-valuenow", cur_value).find("span").html(cur_value + "%");
			// Pretend to do something.
			var timer = setInterval(function() {
				//if (cur_value == 50) {
				//	loader.update({title: "Raising the Sun", icon: "fa fa-sun-o fa-spin"});
				//}
				if (cur_value >= 100) {
					// Remove the interval.
					window.clearInterval(timer);
					loader.remove();
					return;
				}
				cur_value += 1;
				progress.width(cur_value + "%").attr("aria-valuenow", cur_value).find("span").html(cur_value + "%");
			}, 65);
		}
	});

	return loader;
}

function startProgressingBar(init_value) {

	var html = '<div class="modal fade" id="progressing-modal">\
					<div class="modal-dialog">\
						<div class="modal-content">\
							<div class="modal-header">\
								<h4 class="modal-title">Progressing</h4>\
							</div>\
							<div class="modal-body">\
								<div class="progress">\
									<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="1" aria-valuemin="0" aria-valuemax="100" style="width: 1%">\
										<span class="sr-only">1% Complete</span>\
									</div>\
								</div>\
							</div>\
						</div>\
					</div>\
				</div>';

	$('body').append($(html));

	var cur_value = init_value || Math.round(Math.random() * 10);

	$('#progressing-modal').find('.progress-bar').width(cur_value + "%").attr("aria-valuenow", cur_value);

	$('#progressing-modal').modal('show');


	window.progressingTimer = setInterval(function() {

		cur_value += Math.round(Math.random() * 10);

		cur_value = cur_value > 99 ? 99 : cur_value;

		$('#progressing-modal').find('.progress-bar').width(cur_value + '%').attr('aria-valuenow', cur_value)
	}, 500);

}

function stopProgressingBar() {

	window.clearInterval(window.progressingTimer);

	$('#progressing-modal').find('.progress-bar').width("100%").attr("aria-valuenow", "100");

	setTimeout("$('#progressing-modal').modal('toggle')", 1000);

}

function getModalWin(_id, _title, _content_html, large) {

	if ($('#' + _id + '_modal')) {
		$('#' + _id + '_modal').remove();
	}

	var extraConfig =  dialogConfig = '';

	if (large) {
		extraConfig = 'bs-example-modal-lg';
		dialogConfig = 'modal-lg';
	}

	var html = '<div class="modal fade ' + extraConfig + '" id="' + _id + '_modal">\
					<div class="modal-dialog ' + dialogConfig + '">\
						<div class="modal-content">\
							<div class="modal-header">\
								<h4 class="modal-title">' + _title + '</h4>\
							</div>\
							<div class="modal-body">'
								+ _content_html + '\
							</div>\
						</div>\
					</div>\
				</div>';

	$('body').append($(html));

	var _modal_win = $('#' + _id + '_modal');

	//function a(win){
	//
	//	win.show = function() {
	//
	//		if ($('#progressing-modal').is(':visible')) {
	//			setTimeout("win.modal('show')", 1000);
	//		} else {
	//			win.modal('show');
	//		}
	//	}
	//}(_modal_win);

	_modal_win.show = function() {
		_modal_win.modal('show');
	}


	_modal_win.close = function() {
		_modal_win.modal('toggle');
	}

	return _modal_win;
}

function getContextPath() {
	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0,index+1);
	if (result === '/component') return '';
	return result;
}

function getServiceURL(module, method) {
	return document.location.origin + getContextPath() + '/api/s/' + module + '/' + method;
}

function request_FORM(form, url, argument, successHanlder, errorHandler, hideBox) {

	if (!form) {
		noty('error', 'Request Failed', 'Form Invalid!');
		return;
	}

	if (!hideBox) {
		startProgressingBar();
	}

	form.ajaxSubmit({
		type : 'post',
		url : url,
		data : argument,
		contentType : "application/json; charset=utf-8",
		dataType : 'json',
		success: function (data) {
			if (!hideBox) {
				stopProgressingBar();
			}
			if (!data.success) {
				noty('error', 'Request Failed', data.msg);
				return;
			}
			if (successHanlder) {
				successHanlder.apply(successHanlder, [data]);
			}
		},
		error: function (data) {
			if (!hideBox) {
				stopProgressingBar();
			}
			if (errorHandler) {
				errorHandler.apply(errorHandler, [data])
			}
		}
	});

}

function request_AJAX(url, argument, successHanlder, errorHandler, hideBox) {

	if (!hideBox) {
		startProgressingBar();
	}

	$.ajax({
		type : 'post',
		url : url,
		data : argument,
		contentType : "application/json; charset=utf-8",
		dataType : 'json',
		success: function (data) {
			if (!hideBox) {
				stopProgressingBar();
			}
			if (!data.success) {
				noty('error', 'Request Failed', data.msg);
				return;
			}
			if (successHanlder) {
				successHanlder.apply(successHanlder, [data]);
			}
		},
		error: function (data) {
			if (!hideBox) {
				stopProgressingBar();
			}
			if (errorHandler) {
				errorHandler.apply(errorHandler, [data])
			}
		}
	});
}
